import QtQuick 1.0


/*
     There I defined a CheckBox item like a delegate for  To Do list.
     And I implemented behavior on:
                - onDeleteCheckBoxImgClicked
                - onChecked
                - onUnChecked
                - onMoveUp
                - onMoveDown
                - onEditItem

 */

Item {
    id: listToDoDelegate
    width: parent.width
    height: idCh.height

    signal editNextItem

    /*
      move item to checked list
    */        
    function moveToCheckedList()
    {
        listToDoModelUnChecked.setProperty(index, "parentList","toDoListCheckedItems" )
        listToDoModelChecked.append(listToDoModelUnChecked.get(index))        
        listToDoModelUnChecked.remove(index)        
    }

    //change parent list for this item
    /*function updateParentList(parentUpdate)
    {
        var contor=0        
        for(contor=0; contor<items.count; contor++)        
            if(items.get(contor).indexItem == listToDoModelUnChecked.get(index).indexItem)
                items.get(contor).parentList = parentUpdate            
    }*/


    CheckBox
    {
        id:idCh

        text:  title
        width:parent.width
        state: parentList == "toDoListCheckedItems" ? "on" :"off"
        textColor: parentList == "toDoListCheckedItems" ? "grey" :"black"
        upDownVisible: parentList == "toDoListCheckedItems" ? false : true

        onDeleteCheckBoxImgClicked:
            if(parentList == "toDoListUnCheckedItems")
            {
                console.log("parent = "+parentList)
                listUndoDeletion.addItem(title, indexItem, parentList)
                listToDoModelUnChecked.remove(index)                
            }
            else
            {
                listUndoDeletion.addItem(title, indexItem, parentList)
                listToDoModelChecked.remove(index)
            }

        onChecked:        
             listToDoModelUnChecked.moveToCheckedList(index)

        onUnChecked:        
             listToDoModelChecked.moveToList(index,"checkedList", "unCheckedList")
        onMoveUp:
            {
                if(index>0)
                {                    
                    var aux = indexItem                    
                    listToDoModelUnChecked.updateData(index
                                                       , "indexItem"
                                                       ,listToDoModelUnChecked.getIndexItem(index-1)
                                                       )                    
                    listToDoModelUnChecked.updateData(index-1, "indexItem",aux)                    
                    listToDoModelUnChecked.move(index,index-1)

                }
            }
        onMoveDown:
            {
                if( index< (listToDoModelUnChecked.count()-1) )
                {                    
                    var aux = indexItem
                    listToDoModelUnChecked.updateData(index
                                                       , "indexItem"
                                                       ,listToDoModelUnChecked.getIndexItem(index+1)
                                                       )                    

                    listToDoModelUnChecked.updateData(index+1, "indexItem",aux)
                    listToDoModelUnChecked.move(index,index+1)

                }
            }

        onEditItem:
        {            
            //listModelTaDaList.updateTitle(int fromIndex, int itemIndex, QString newTitle)            
            if(parentList == "toDoListUnCheckedItems")                
            {
                listToDoModelUnChecked.updateData(index, "title", idCh.textFromEdit)
                //console.log("index curent="+index)
                //toDoListUnCheckedItems.incrementCurrentIndex
                //console.log("index curent="+index)

            }
            else
                listToDoModelChecked.updateData(index, "title", idCh.textFromEdit)

        }
     }
}
